package com.ruoyi.web.controller.erp.coordinatorgroup;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.erp.coordinatorgroup.domain.OrderCoordinatorGroupTab;
import com.ruoyi.erp.coordinatorgroup.service.CoordinatorGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/coordinator_group_dao")
@Api(value = "协助查询:")
public class CoordinatorGroupController extends BaseController {
  @Autowired
  private CoordinatorGroupService coordinatorGroupService;


  /**
   * 查询列表
   */
  @PreAuthorize("@ss.hasPermi('coordinatorGroup:coordinatorgroup:list')")
  @PostMapping("/select_coordinatorgroup")
  @ResponseBody
  public TableDataInfo getList(@Validated @RequestBody OrderCoordinatorGroupTab orderCoordinatorGroupTab){
    List<OrderCoordinatorGroupTab> orderCoordinatorGroupTabList = coordinatorGroupService.getList(orderCoordinatorGroupTab);
    return getDataTable(orderCoordinatorGroupTabList);
  }

  @PreAuthorize("@ss.hasPermi('coordinatorGroup:coordinatorgroup:add')")
  @ApiOperation(value="插入：insert_coordinatorgroup",httpMethod = "POST")
  @RequestMapping("/insert_coordinatorgroup")
  @ResponseBody
  public AjaxResult insertCoordinator(@Validated @RequestBody OrderCoordinatorGroupTab orderCoordinatorGroupTab) {
    try{
      orderCoordinatorGroupTab.setRowkey(UUID.randomUUID().toString());
      coordinatorGroupService.insertCoordinator(orderCoordinatorGroupTab);
    }catch (Exception e) {
      e.printStackTrace();
      System.err.println(e.getMessage());
      return AjaxResult.error("添加失败");
    }

    return AjaxResult.success("添加成功");
  }

  @PreAuthorize("@ss.hasPermi('coordinatorGroup:coordinatorgroup:remove')")
  @Log(title = "删除协调人组信息", businessType = BusinessType.DELETE)
  @ApiOperation(value="删除：delete_coordinatorgroup",httpMethod = "POST")
  @PostMapping("/delete_coordinatorgroup/{ids}")
  @ResponseBody
  public AjaxResult deleteCoordinatorgroups(@PathVariable  @RequestBody String [] ids){
    return toAjax(coordinatorGroupService.deleteCoordinator(ids));
  }

  @PreAuthorize("@ss.hasPermi('coordinatorGroup:coordinatorgroup:edit')")
  @ApiOperation(value="更新：update_coordinatorgroup",httpMethod = "POST")
  @PostMapping("/update_coordinatorgroup")
  @ResponseBody
  public AjaxResult updateCoordinatorgoups(@Validated @RequestBody OrderCoordinatorGroupTab orderCoordinatorGroupTab) {
    try {
      coordinatorGroupService.updateCoordinator(orderCoordinatorGroupTab);
    } catch (Exception e) {
      e.printStackTrace();
      return AjaxResult.success("修改失败");
    }
    return AjaxResult.success("修改成功");
  }

  @Log(title = "协调人组",businessType = BusinessType.EXPORT)
  @PreAuthorize("@ss.hasPermi('coordinatorGroup:coordinatorgroup:export')")
  @PostMapping("/export")
  @ResponseBody
  public AjaxResult export(@Validated @RequestBody OrderCoordinatorGroupTab orderCoordinatorGroupTab){
    List<OrderCoordinatorGroupTab> orderCoordinatorGroupTabList = coordinatorGroupService.getList(orderCoordinatorGroupTab);
    ExcelUtil<OrderCoordinatorGroupTab> util = new ExcelUtil<OrderCoordinatorGroupTab>(OrderCoordinatorGroupTab.class);
    return util.exportExcel(orderCoordinatorGroupTabList, "协调人组");
  }


  @PreAuthorize("@ss.hasPermi('coordinatorGroup:coordinatorgroup:query')")
  @ApiOperation(value="根据authorizeGroup查询详情")
  @GetMapping("/edit/{authorizeGroup}")
  @ResponseBody
  public AjaxResult getInfo(@PathVariable String authorizeGroup)
  {
    return AjaxResult.success(coordinatorGroupService.findOrderCoordinatorGroupTab(authorizeGroup));
  }
}
